package com.atguigu.tingshu.user.service.impl;

import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.*;
import com.atguigu.tingshu.user.mapper.*;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.user.strategy.StrateFactory;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.LocalDateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

	@Autowired
	private UserInfoMapper userInfoMapper;

	@Autowired
	private UserPaidAlbumMapper userPaidAlbumMapper;

	@Autowired
	private UserPaidTrackMapper userPaidTrackMapper;

	@Autowired
	private UserVipServiceMapper userVipServiceMapper;

	@Autowired
	private VipServiceConfigMapper vipServiceConfigMapper;

	@Autowired
	private TrackInfoFeignClient trackInfoFeignClient;

	//获取登录用户信息
	@Override
	public UserInfoVo getUserInfo(Long userId) {
		UserInfo userInfo = userInfoMapper.selectById(userId);
		UserInfoVo userInfoVo = new UserInfoVo();
		BeanUtils.copyProperties(userInfo,userInfoVo);
		return userInfoVo;
	}

	//判断用户是否购买声音
	@Override
	public Map<Long, Integer> userIsPaidTrack(Long userId, Long albumId, List<Long> trackIdList) {
		//1 根据用户id 和 专辑id 查询用户是否购买这个专辑
		LambdaQueryWrapper<UserPaidAlbum> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(UserPaidAlbum::getUserId,userId).eq(UserPaidAlbum::getAlbumId,albumId);
		UserPaidAlbum userPaidAlbum = userPaidAlbumMapper.selectOne(wrapper);
		if(userPaidAlbum!=null){
			//表明用户购买过这个专辑
			Map<Long, Integer> hashMap = new HashMap<>();
			trackIdList.forEach(trackid->{
				hashMap.put(trackid,1);

			});
			return hashMap;
		}else{
			//表明用户没有购买这个专辑
			//查询用户购买过哪些声音
			LambdaQueryWrapper<UserPaidTrack> wrapper1 = new LambdaQueryWrapper<>();
			wrapper1.eq(UserPaidTrack::getUserId,userId).in(UserPaidTrack::getTrackId,trackIdList);
			List<UserPaidTrack> userPaidTrackList = userPaidTrackMapper.selectList(wrapper1);
			Map<Long, Integer> map = new HashMap<>();
			trackIdList.forEach(trackId->{

				if(userPaidTrackList.contains(trackId)){
					map.put(trackId,1);
				}else{
					map.put(trackId,0);
				}
			});
			return map;
		}

	}

	//判断用户是否购买过专辑
	@Override
	public Boolean isPaidAlbum(Long itemId, Long userId) {
		LambdaQueryWrapper<UserPaidAlbum> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(UserPaidAlbum::getAlbumId,itemId);
		wrapper.eq(UserPaidAlbum::getUserId,userId);
		Long count = userPaidAlbumMapper.selectCount(wrapper);
		if(count!=0){
			//表明查询到了数据，表示用户购买过专辑
			return true;
		} else{
			return false;
		}

	}

	//查询用户购买过的声音列表
	@Override
	public List<Long> findUserPaidTrackId(Long userId, Long albumId) {
		LambdaQueryWrapper<UserPaidTrack> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(UserPaidTrack::getUserId,userId);
		wrapper.eq(UserPaidTrack::getAlbumId,albumId);
		List<UserPaidTrack> userPaidTrackList = userPaidTrackMapper.selectList(wrapper);
		List<Long> trackIdList = userPaidTrackList.stream()
						.map(UserPaidTrack::getId).collect(Collectors.toList());
		return trackIdList;
	}

	//添加购买记录
//	@Override
//	public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
//		//根据购买商品的不同类型添加不同表的数据
//		if(userPaidRecordVo.getItemType().equals(SystemConstant.ORDER_ITEM_TYPE_ALBUM)){
//			//购买专辑
//			//判断当前订单专辑是否已经添加(根据专辑id和用户id查询已经购买专辑表看是否能够查询的到)
//			LambdaQueryWrapper<UserPaidAlbum> wrapper = new LambdaQueryWrapper<>();
//			wrapper.eq(UserPaidAlbum::getUserId,userPaidRecordVo.getUserId());
//			wrapper.eq(UserPaidAlbum::getAlbumId,userPaidRecordVo.getItemIdList().get(0));
//
//			UserPaidAlbum selectOne = userPaidAlbumMapper.selectOne(wrapper);
//			if (selectOne==null){
//				//表示该专辑是第一次添加
//				UserPaidAlbum userPaidAlbum = new UserPaidAlbum();
//				userPaidAlbum.setUserId(userPaidRecordVo.getUserId());
//				userPaidAlbum.setAlbumId(userPaidRecordVo.getItemIdList().get(0));
//				userPaidAlbum.setOrderNo(userPaidRecordVo.getOrderNo());
//				userPaidAlbumMapper.insert(userPaidAlbum);
//			}else {
//				return;
//			}
//
//
//
//		}else if (userPaidRecordVo.getItemType().equals(SystemConstant.ORDER_ITEM_TYPE_TRACK)){
//			//购买声音
//			//判断当前用户+声音记录是否已经添加(根据用户id和订单号)
//			LambdaQueryWrapper<UserPaidTrack> userPaidTrackWrapper = new LambdaQueryWrapper<>();
//			userPaidTrackWrapper.eq(UserPaidTrack::getUserId,userPaidRecordVo.getUserId());
//			userPaidTrackWrapper.eq(UserPaidTrack::getOrderNo,userPaidRecordVo.getOrderNo());
//
//			UserPaidTrack userPaidTrack = userPaidTrackMapper.selectOne(userPaidTrackWrapper);
//			if (userPaidTrack!=null){
//				//添加过生意
//				return;
//			}
//		//远程调用：根据声音id获取声音数据
//					Long trackId = userPaidRecordVo.getItemIdList().get(0);
//					Result<TrackInfo> trackInfoResult =  trackInfoFeignClient.getTrackInfo(trackId);
//					TrackInfo trackInfo = trackInfoResult.getData();
//					//根据声音id获取专辑id
//					Long albumId = trackInfo.getAlbumId();
//			userPaidRecordVo.getItemIdList().stream().forEach(itemId->{
//				UserPaidTrack userPaidTrack1 = new UserPaidTrack();
//				userPaidTrack1.setUserId(userPaidRecordVo.getUserId());
//				userPaidTrack1.setOrderNo(userPaidRecordVo.getOrderNo());
//				userPaidTrack1.setTrackId(itemId);
//				userPaidTrack1.setAlbumId(albumId);
//				userPaidTrackMapper.insert(userPaidTrack1);
//			});
//
//
//		} else if (userPaidRecordVo.getItemType().equals(SystemConstant.ORDER_ITEM_TYPE_VIP)) {
//			//购买vip
//
//			Long userId = userPaidRecordVo.getUserId();
//
//			LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
//			queryWrapper.eq(UserInfo::getId,userId);
//			UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
//			Integer isVip = userInfo.getIsVip();
//			Date vipExpireTime = userInfo.getVipExpireTime();
//			if(isVip.intValue()==0 || vipExpireTime.before(new Date())){
//				//如果之前没有购买过vip或是购买过但是已经过期，那么直接添加数据
//				UserVipService userVipService = new UserVipService();
//				userVipService.setUserId(userId);
//				userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
//				Long vipId = userPaidRecordVo.getItemIdList().get(0);
//				VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipId);
//				Integer serviceMonth = vipServiceConfig.getServiceMonth();
//				Date date = new Date();
//				Date newExpireTime = new LocalDateTime(date).plusMonths(serviceMonth).toDate();
//				userVipService.setStartTime(new Date());
//				userVipService.setExpireTime(newExpireTime);
//				userVipServiceMapper.insert(userVipService);
//				//修改用户表vip状态
//				userInfo.setIsVip(1);
//				//设置过期时间
//				userInfo.setVipExpireTime(newExpireTime);
//				//修改
//				userInfoMapper.updateById(userInfo);
//
//			}else if (isVip==1 && vipExpireTime.after(new Date())){
//				//如果是vip并且没有过期
//				//如果之前购买过vip并且没有过期，那么在vip过期时间上直接续期
//				UserVipService userVipService = new UserVipService();
//				userVipService.setUserId(userId);
//				userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
//				Long vipId = userPaidRecordVo.getItemIdList().get(0);
//				VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipId);
//				Integer serviceMonth = vipServiceConfig.getServiceMonth();
//				Date newExpireTime = new LocalDateTime(vipExpireTime).plusMonths(serviceMonth).toDate();
//				userVipService.setStartTime(new Date());
//				userVipService.setExpireTime(newExpireTime);
//				userVipServiceMapper.insert(userVipService);
//				//修改用户表vip状态
//				userInfo.setIsVip(1);
//				//设置过期时间
//				userInfo.setVipExpireTime(newExpireTime);
//				//修改
//				userInfoMapper.updateById(userInfo);
//			}
//
//
//
//		}
//	}
//
	@Autowired
	private StrateFactory strateFactory;
	//添加购买记录
	@Override
	public void savePaidRecord(UserPaidRecordVo userPaidRecordVo){
		String itemType = userPaidRecordVo.getItemType();
		ItemTypeStrategy strategy = strateFactory.getStrategy(itemType);
		strategy.savePaidRecord(userPaidRecordVo);
	}
}
